import requests

def login(username,password):
    payload = {
        'username': username,
        'password': password
    }
    # data参数 就是构造消息体的
    response = requests.post("http://localhost/api/mgr/loginReq",
                             data=payload)

    # 获取结果，返回给调用者
    retDict = response.json()
    # 打印出结果
    print(retDict)

    return retDict,response.cookies

def add_teacher(username,password,realname,desc):
    pl = {
        'action': 'add_teacher',
        'data' : '''
                {
                    "username":"%s",
                    "password":"%s",
                    "realname":"%s",
                    "desc":"%s",
                    "courses":[{"id":13,"name":"c++"}],
                    "display_idx":1
                }
        ''' %  (username,password,realname,desc,)
    }


    r = requests.post('http://localhost/api/mgr/sq_mgr/',
                            data=pl,
                            cookies={'sessionid': sessionid})

    retDict = r.json()
    print(retDict)
    return retDict

loginRet,cookies = login('auto','sdfsdfsdf')
assert loginRet['retcode']==0
sessionid = cookies['sessionid']
add_teacher('laoli','sq888','老李','laoli')


import MySQLdb
my = MySQLdb.connect(host='192.168.230.128',
                     user='songqin',
                     passwd='songqin',
                     db="plesson",
                     charset='utf8')
cursors = my.cursor()
cursors.execute('''SELECT * FROM sq_teacher where realname= '老李'  ''' )
data = cursors.rowcount
print(data)
data = cursors.fetchone()
print( data)

print('\n-------------test ceae pass---------------')

# my.close()




